﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;

namespace Fishes.Objects
{
    public class ObjectProperty
    {
        #region Events

        public delegate void OnDecrementDelegate(ObjectProperty op);
        public event OnDecrementDelegate OnDecrement;

        #endregion

        #region Properties

        public int Value
        {
            get { return _value; }
            set { _value = value; }
        }
        private int _value = 0;

        public int CurrentValue
        {
            get { return _currentValue; }
            set { _currentValue = value; }
        }
        private int _currentValue = 0;

        public int DecrementAmount
        {
            get { return _decrementAmount; }
            set { _decrementAmount = value; }
        }
        private int _decrementAmount = 0;

        public TimeSpan DecrementTime
        {
            get { return _decrementTime; }
            set { _decrementTime = value; }
        }
        private TimeSpan _decrementTime = TimeSpan.Zero;

        private TimeSpan LastUpdateTime
        {
            get { return _lastUpdateTime; }
        }
        private TimeSpan _lastUpdateTime = TimeSpan.Zero;

        #endregion

        #region Public Methods

        public bool Load(XmlDocument doc, string root)
        {
            try
            {
                this._value = Convert.ToInt32(doc.SelectSingleNode(String.Format("{0}/value", root)).InnerText);
                this._currentValue = this.Value;

                this._decrementAmount = Convert.ToInt32(doc.SelectSingleNode(String.Format("{0}/decrementAmount", root)).InnerText);
                this._decrementTime = new TimeSpan(0, 0, 0, 0,
                    Convert.ToInt32(doc.SelectSingleNode(String.Format("{0}/decrementTime", root)).InnerText));
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

        public void Update(GameTime gt)
        {
            this._lastUpdateTime = this._lastUpdateTime.Add(gt.ElapsedGameTime);
            if (this._lastUpdateTime > this.DecrementTime)
            {
                if (this.OnDecrement != null)
                    this.OnDecrement(this);

                this._currentValue -= this._decrementAmount;
                this._lastUpdateTime = TimeSpan.Zero;
            }
        }

        #endregion
    }
}
